package com.sophialex.health.record.action;

import java.util.Iterator;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;

import com.sophia.common.xmlbean.util.BaseXMLPojo;
import com.sophia.common.xmlbean.util.PojoXMLUtil;
import com.sophialex.health.common.platform.SuperContext;
import com.sophialex.health.person.vo.HrPerson;
import com.sophialex.health.person.vo.SoapService;
import com.sophialex.health.person.vo.SoapServiceDetail;
import com.sophialex.health.record.Messages;
import com.sophialex.health.record.common.SOAPServiceDispath;
import com.sophialex.health.record.common.util.DataSource;
import com.sophialex.health.record.common.utils.ImageUtil;
import com.sophialex.health.record.views.BaseView;
import com.sophialex.health.record.views.PersonBottomRightView.TreeObject;
import com.sophialex.health.system.frame.dialog.DialogManager;
import com.sophialex.health.system.frame.util.ViewUitl;
/**
 * 打开服务页面
 * <p>Title:Health</p>
 * <p>OpenSoapServiceAction</p>
 * 
 * Alex Apr 24, 2011 7:28:05 PM create
 * @author <a href='wanghao924@126.com'>Alex</a>
 * QQ: 873987060;
 * @version 1.0
 */
public class OpenSoapServiceAction extends Action implements IWorkbenchAction {
    public static final String ID = "com.sophialex.health.record.action.OpenSoapServiceAction";
    
    private IWorkbenchWindow workbenchWindow;
    
    private TreeViewer soapServiceTreeViewer;
    
    public OpenSoapServiceAction(IWorkbenchWindow workbenchWindow,TreeViewer treeViewer){
    	if(workbenchWindow==null){
    		throw new IllegalArgumentException();
    	}
    	this.soapServiceTreeViewer = treeViewer;
    	this.workbenchWindow = workbenchWindow;
    	setId(ID);
    	setText(Messages.getString("OpenSoapServiceAction.0"));
    	setImageDescriptor(ImageUtil.edit_issue);
    }
    
	public void dispose() {
		workbenchWindow = null;
	}
	
	public void run(){
		if(workbenchWindow == null){
			return;
		}
		HrPerson currentPerson = SuperContext.currentPerson;
		if(currentPerson == null){
			DialogManager.invokeWarnDlg(Messages.getString("Common.12"));
			return;
		}
		if(soapServiceTreeViewer.getSelection().isEmpty()){
			DialogManager.invokeInfoDlg(Messages.getString("OpenSoapServiceAction.1"));
		}else{
			if(soapServiceTreeViewer.getSelection() instanceof IStructuredSelection) {
				IStructuredSelection selection = (IStructuredSelection) soapServiceTreeViewer.getSelection();
				for(Iterator iterator = selection.iterator(); iterator.hasNext();) {
					Object element = iterator.next();
					if(element instanceof TreeObject) {
	                       TreeObject treeObject = (TreeObject) element;
	                       if(treeObject.getBusType().longValue() != 1){
	                    	   DialogManager.invokeInfoDlg(Messages.getString("OpenSoapServiceAction.1"));
	                    	   
	                       }else{
	                    	   SoapService soapService = treeObject.getSoapService();
	                    	   SoapServiceDetail detail = DataSource.soapServiceDetail.getSoapServiceDetailBySoapId(soapService.getId());
	                    	   BaseXMLPojo baseXmlPojo=null;
	                           if(detail != null){
	                        	  baseXmlPojo = PojoXMLUtil.XMLToPojo(detail.getContent(),SOAPServiceDispath.serviceClassMap.get(soapService.getBusType()));
	                           }
	                    	   BaseView baseView = (BaseView) ViewUitl.showView(SOAPServiceDispath.serviceMap.get(soapService.getBusType()));
	                    	   if(baseXmlPojo != null){
	                    		   baseView.refershView(baseXmlPojo,soapService,detail);
	                    	   }
	                       }
					}else{
						DialogManager.invokeInfoDlg(Messages.getString("OpenSoapServiceAction.1"));
					}
				
				}

			}
		}
		
		
	}

}
